﻿using Common;
using IBLL;
using IDAL;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class CustomerInfoBLL : ICustomerInfoBLL
    {
        private ICustomerInfoDAL _customerInfoDAL;
        private ICustomerAddressDAL _customerAddressDAL;

        public CustomerInfoBLL(
            ICustomerInfoDAL customerInfoDAL
            , ICustomerAddressDAL customerAddressDAL
            )
        {
            _customerInfoDAL = customerInfoDAL;
            _customerAddressDAL = customerAddressDAL;
        }

        #region 登录的方法 (Login)
        /// <summary>
        /// 登录的方法
        /// </summary>
        /// <param name="customerAccount"></param>
        /// <param name="customerPassword"></param>
        /// <param name="msg"></param>
        /// <param name="customerName"></param>
        /// <param name="customertId"></param>
        /// <returns></returns>
        public bool LoginIndex(string customerAccount, string customerPassword, out string msg, out string customerName, out string customertId)
        {
            try
            {
                string newPasswork = MD5help.Md5(customerPassword);
                CustomerInfo customerInfo = _customerInfoDAL.GetEntities().FirstOrDefault(it => it.CustomerAccount == customerAccount && it.CustomerPassword == newPasswork);

                customerName = "";
                customertId = "";
                if (customerInfo == null)
                {
                    msg = "账号或者密码错误";
                    return false;
                }
                else
                {
                    msg = "成功";
                    customerName = customerInfo.CustomerName;
                    customertId = customerInfo.Id;
                    return true;
                }
            }
            catch (Exception ex)
            {
                customerName = "";
                customertId = "";
                msg = "网络异常请检查网络" + ex;
                return false;
            }
        }
        #endregion

        #region 获取顾客表的方法 (GetCustomerInfos)
        /// <summary>
        /// 获取顾客表的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="departmentName"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<GetCustomerInfoDTO> GetCustomerInfo(int page, int limit, string customerName, out int count)
        {
            var data = from c in _customerInfoDAL.GetEntities().Where(d => d.IsDelete == false)
                       join ca in _customerAddressDAL.GetEntities()
                       on c.AddressId equals ca.Id
                       into CCAtemp
                       from cca in CCAtemp.DefaultIfEmpty()
                       select new GetCustomerInfoDTO
                       {
                           Id = c.Id,
                           CustomerName = c.CustomerName,
                           CustomerAccount = c.CustomerAccount,
                           CustomerPassword = c.CustomerPassword,
                           CustomerEmail = c.CustomerEmail,
                           CustomerPhone = c.CustomerPhone,
                           AddressId = cca.Address,
                           CreateTime = c.CreateTime
                       };

            if (!string.IsNullOrWhiteSpace(customerName))
            {
                //条件精准查询
                data = data.Where(d => d.CustomerName == customerName);
            }

            //计算数据总数
            count = data.Count();
            //分页(降序)
            var listpage = data.OrderByDescending(u => u.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();

            return listpage;
        }
        #endregion

        #region 创建顾客 (CreateCustomerInfo)
        /// <summary>
        /// 创建顾客
        /// </summary>
        /// <param name="cust">用户实体</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        public bool CreateCustomerInfo(CustomerInfo cust, out string msg)
        {
            if (string.IsNullOrWhiteSpace(cust.CustomerName))
            {
                msg = "顾客名称不能为空";
                return false;
            }
            //验证顾客是否存在
            if (_customerInfoDAL.GetEntities().FirstOrDefault(it => it.CustomerName == cust.CustomerName) != null)
            {
                msg = "顾客已经存在";
                return false;
            }
            cust.Id = Guid.NewGuid().ToString();//用户id
            cust.CreateTime = DateTime.Now;//设置时间
            cust.CustomerPassword = MD5help.Md5(cust.CustomerPassword);
            bool IsSuccess = _customerInfoDAL.CreateEntity(cust);//调用方法
            msg = IsSuccess ? $"添加{cust.CustomerName}成功" : "添加用户失败";

            return IsSuccess;
        }
        #endregion

        #region 删除的方法 (DeleteCustomerInfo)
        /// <summary>
        /// 删除的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteCustomerInfo(string id)
        {
            CustomerInfo cust = _customerInfoDAL.GetEntityById(id);
            if (cust == null)
            {
                return false;
            }
            cust.IsDelete = true;
            cust.DeleteTime = DateTime.Now;
            _customerInfoDAL.UpdateEntity(cust);
            return true;
        }
        #endregion

        #region 批量删除的方法 (DeleteListCustomerInfo)
        /// <summary>
        /// 批量删除的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteListCustomerInfo(List<string> ids)
        {
            List<CustomerInfo> custList = _customerInfoDAL.GetEntities().Where(it => ids.Contains(it.Id)).ToList();
            foreach (var item in ids)
            {
                CustomerInfo cust = _customerInfoDAL.GetEntityById(item);
                if (custList == null)
                {
                    return false;
                }
                cust.IsDelete = true;
                cust.DeleteTime = DateTime.Now;

                _customerInfoDAL.UpdateEntity(cust);
            }
            return true;
        }
        #endregion

        #region 修改的方法 (UpdateCustomerInfo)
        /// <summary>
        /// 修改用户的方法
        /// </summary>
        /// <param name="cust"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool UpdateCustomerInfo(CustomerInfo cust, out string msg)
        {
            if (string.IsNullOrWhiteSpace(cust.CustomerName))
            {
                msg = $"顾客名称不能为空";
                return false;
            }
            CustomerInfo entity = _customerInfoDAL.GetEntityById(cust.Id);
            if (entity.Id == null)
            {
                msg = "ID不存在";
                return false;
            }
            //判断重复
            if (entity.CustomerName != cust.CustomerName)
            {
                var data = _customerInfoDAL.GetEntities().FirstOrDefault(it => it.CustomerName == cust.CustomerName);
                if (data != null)
                {
                    msg = "顾客名已经被占用";
                    return false;
                }
            }

            entity.CustomerName = cust.CustomerName;
            entity.CustomerAccount = cust.CustomerAccount;
            entity.CustomerPassword = cust.CustomerPassword;
            entity.CustomerEmail = cust.CustomerEmail;
            entity.CustomerPhone = cust.CustomerPhone;
            entity.AddressId = cust.AddressId;

            bool IsSuccess = _customerInfoDAL.UpdateEntity(entity);//调用方法

            msg = IsSuccess ? $"修改成功" : "修改失败";

            return IsSuccess;
        }
        #endregion

        #region 根据ID获取数据返回赋值 (GetCustomerInfoById)
        /// <summary>
        /// 根据ID获取数据返回赋值
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CustomerInfo GetCustomerInfoById(string id)
        {
            return _customerInfoDAL.GetEntityById(id);
        }
        #endregion

        #region 获取数据库列表赋值下拉框 (GetSelectOption)
        /// <summary>
        /// 获取主管和父级顾客下拉框的方法
        /// </summary>
        /// <returns></returns>
        public object GetSelectOption()
        {
            var addressData = _customerAddressDAL.GetEntities().Where(it => it.Id != null).Select(d => new
            {
                value = d.Id,
                title = d.Address
            });

            return new
            {
                addressData = addressData,
            };
        }
        #endregion
    }
}
